package com.example.helloandroid;

import java.util.LinkedList;
import java.util.List;

import com.example.helloandroid.Card;

// Notes: Hands will be arranged, Clubs, Spades, Hearts, Diamonds for 1-52.
public class Deck {
	private LinkedList<Card> cards;
	
	public void generateCards()
	{
		cards = new LinkedList<Card>();
		for(int i = 1; i <= 52; i++)
		{
			Card card = new Card();
			card.set((int)Math.floor((double)i/13.0), i % 13);
			cards.add(card);
		}
	}
	
	public void shuffle()
	{
		LinkedList<Integer> tempCardList = new LinkedList<Integer>();
		LinkedList<Integer> shuffledList = new LinkedList<Integer>();
		for(int i = 1; i <= 52; i++)
		{
			tempCardList.add(new Integer(i));			
		}
		
		// Shuffle the cards
		while(!tempCardList.isEmpty())
		{
			int randIndex = (int)Math.floor( Math.random() * (double)(tempCardList.size()) );	// generates a random index
			shuffledList.add(tempCardList.get(randIndex));	// push item at randIndex
			tempCardList.remove(randIndex);					// remove item at ri
		}
		cards.clear();
		// Assign the cards to our deck
		for(int i = 0; i < shuffledList.size(); i++)
		{
			Card card = new Card();
			int v = shuffledList.get(i);
			card.set((int)Math.ceil(v/13.0), v % 13);	// set card value
			cards.add(card);							// add to cards
		}
	}
	
	public LinkedList<Card> getCards(int start, int end)
	{		
		LinkedList<Card> lst = new LinkedList<Card>();
		List<Card> tempList;
		
		if (end > cards.size()){
			tempList = cards.subList(start, cards.size());
		}
		else if (start < 0){
			tempList = cards.subList(0, end+1);
		}
		else
		{
			tempList = cards.subList(start, end+1);
		}
		
		// Assign cards manually to the temp list
		for(int i = 0; i < tempList.size(); i++)
		{
			Card card = new Card();
			card.set(tempList.get(i).getType(), tempList.get(i).getValue());
			lst.add(card);
		}
		
		return lst;
	}
}